﻿(function () {
    var _inited = false;
    var _handler = "../Handlers/UserInfoHandler.ashx";

    function _init() {
        _initUI();
        _initDetailUI();
        _getDataGroups();
    }

    function _checkVal() {
        if ($.trim($('#txtUserName', 'table.tbdetail').val()) == "") {
            alert('请输入用户名');
            return false;
        }
        if ($.trim($('#txtPassword', 'table.tbdetail').val()) == "") {
            alert('请输入密码');
            return false;
        }
        if ($.trim($('#txtPasswordConfirm', 'table.tbdetail').val()) == "") {
            alert('请确认密码');
            return false;
        }
        if ($.trim($('#txtPassword', 'table.tbdetail').val()) != $.trim($('#txtPasswordConfirm', 'table.tbdetail').val())) {
            alert('请输入一致的密码');
            return false;
        }
        return true;
    }

    function _handlerMsg(msg) {
        switch (msg) {
            case 0:
                alert('操作成功');
                _init();
                break;
            case 100:
                alert('用户名相同');
                break;
            case 200:
                alert('旧口令不正确');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _initUI() {
        $('#btnAdd', 'table.tbdetail').die().live('click', function () {
            if (_checkVal()) {
                var params = {};
                params.UserName = $.trim($('#txtUserName', 'table.tbdetail').val());
                params.Password = $.trim($('#txtPassword', 'table.tbdetail').val());
                params.GroupID = $('#ddlUserGroup', 'table.tbdetail').val();
                _addData(params);
            }
        });
        $('#btnDel', 'table.tbdetail').die().live('click', function () {
            var params = $('table.tbdetail').data('data');
            _delData(params);
        });
        $('#btnSave', 'table.tbdetail').die().live('click', function () {
            if (_checkVal()) {
                var params = $('table.tbdetail').data('data');
                params.OldPassword = params.Password;
                params.UserName = $.trim($('#txtUserName', 'table.tbdetail').val());
                params.Password = $.trim($('#txtPassword', 'table.tbdetail').val());
                params.GroupID = $('#ddlUserGroup', 'table.tbdetail').val();
                _saveData(params);
            }
        });
        $('#btnCancel', 'table.tbdetail').die().live('click', function () {
            _initDetailUI();
        });
        $('span.file').die().live('click', function () {
            var li = $(this).closest('li');
            _initDetailUI(li.data('data'));
        });
    }

    function _initDetailUI(cd) {
        if (cd && cd != null) {
            $('#txtUserName', 'table.tbdetail').val(cd.UserName);
            $('#txtPassword', 'table.tbdetail').val(cd.Password);
            $('#txtPasswordConfirm', 'table.tbdetail').val(cd.Password);
            $('#ddlUserGroup', 'table.tbdetail').val(cd.GroupID);
            $('#btnAdd', 'table.tbdetail').attr('disabled', 'disabled');
            if (cd.UserName != 'admin') {
                $('#btnDel', 'table.tbdetail').removeAttr('disabled');
            }
            else {
                $('#btnDel', 'table.tbdetail').attr('disabled', 'disabled');                
            }

            $('#btnSave', 'table.tbdetail').removeAttr('disabled');
            $('table.tbdetail').data('data', cd);
        }
        else {
            $('#txtUserName', 'table.tbdetail').val('');
            $('#txtPassword', 'table.tbdetail').val('');
            $('#txtPasswordConfirm', 'table.tbdetail').val('');
            $('#btnAdd', 'table.tbdetail').removeAttr('disabled');
            $('#btnDel', 'table.tbdetail').attr('disabled', 'disabled');
            $('#btnSave', 'table.tbdetail').attr('disabled', 'disabled');
            $('table.tbdetail').data('data', null);
        }
    }

    function _getDataGroups() {
        $.ajax({
            type: "get",
            url: _handler + "?action=GetUserGroup" + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        $("#usertree").empty();
                        if (data) {
                            for (var i = 0; i < data.length; i++) {
                                var d = data[i];
                                var li = "<li class='closed' style='white-space: nowrap;'>";
                                li += "<span class='folder'>";
                                li += "<label id='Label" + d.AutoID + "' code='" + d.AutoID + "'>";
                                li += d.GroupName;
                                li += "</label>";
                                li += "</span>";
                                li += "<ul code='" + d.AutoID + "'></ul>";
                                li += "</li>";
                                $(li).data('data', d).appendTo($('#usertree'));
                            }
                        }
                        _initTree();
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _initTree() {
        $('.filetree').treeview({
            collapsed: false,
            animated: "medium",
            persist: "location",
            toggle: function () {
                var currentobj = $(this);
                if (!currentobj.is(":hidden")) {
                    var ul = currentobj;
                    _getDataUsers(ul);
                }
            },
            nodeclick: function () {
                _initDetailUI();
            }
        });
    }

    function _getDataUsers(ul) {
        var code = ul.attr("code");
        $.ajax({
            type: "get",
            url: _handler + "?action=GetUserInfo&Params=" + code + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        ul.empty();
                        if (data) {
                            for (var i = 0; i < data.length; i++) {
                                var d = data[i];
                                var li = "<li class='closed' style='white-space: nowrap;'>";
                                li += "<span class='file'>";
                                li += "<label id='Label" + d.AutoID + "' code='" + d.AutoID + "'>";
                                li += d.UserName;
                                li += "</label>";
                                li += "</span>";
                                li += "</li>";
                                $(li).data('data', d).appendTo(ul);
                            }
                        }
                        $("#usertree").treeview({
                            add: branches
                        });
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _addData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=AddUserInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _delData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelUserInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _saveData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveUserInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    this.UserInfoMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        }
    }
})();
$(function () {
    UserInfoMgr.init();
});